import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import { GetCartListApi, AddToCartApi, DelToCartApi } from '@/service/index.js'
export const useCartStore = defineStore('cartStore', () => {
  const cartList = ref({
		coupon_amount: "0",
		delivery_amount: "0",
		delivery_money: "0",
		discount: '0',
		express_money: "0",
		give_integral_true: "0",
		goods_amount: "0",
		items: [],
		pay_amount: "0",
		shop_total_amount: "0",
		total_amount: "0",
	})
	const GETCARTLIST = () => {
		GetCartListApi().then(res => {
			const shopArr = []
			if(res.items) {
				res.items.forEach(item => {
					shopArr.push(item)
				})
				shopArr.forEach(item => {
					item.goods.forEach(child => {
						cartList.value.items.forEach(cart => {
							cart.goods.forEach(cChild => {
								if(child.sku == cChild.sku) {
									child.checked = cChild.checked
								}
							})
						})
					})
				})
				cartList.value = res
				cartList.value.items = shopArr
			} else {
				cartList.value = {
					coupon_amount: "0",
					delivery_amount: "0",
					delivery_money: "0",
					discount: '0',
					express_money: "0",
					give_integral_true: "0",
					goods_amount: "0",
					items: [],
					pay_amount: "0",
					shop_total_amount: "0",
					total_amount: "0"
				}
			}

		})
	}
	/**
	 * @param sku 商家sku ['S241106417','S241106417'] 
	*/
  const DELCARTGOODS = (sku) => {
		let arr = [sku]
		DelToCartApi({sku: arr}).then(res => {
			GETCARTLIST()
		})
	}
	// 购物车数量
	const cartNum = computed(() => {
		let num = 0
		if(cartList.value.items.length==0) return 0
		cartList.value.items.forEach(item => {
			item.goods.forEach(child => {
				num = num + child.num
			})
		})
		return num
	})
	// 选中的数量
	const checkedNum = computed(() => {
		let num = 0
		cartList.value.items.forEach(item => {
			item.goods.forEach(child => {
				if (child.checked) num = num + 1
			})
		})
		return num
	})
	// 选中的价格
	const checkedPrice = computed(() => {
		let price = 0
		cartList.value.items.forEach(item => {
			item.goods.forEach(child => {
				if (child.checked) {
					price = price + Number(child.amount)
				}
			})
		})
		return price
	})
	// 选中的商品
	const checkedList = computed(() => {
		let list = []
		cartList.value.items.forEach(item => {
			item.goods.forEach(child => {
				if (child.checked) list.push(child)
			})
		})
		return list
	})
	// 选中所有商品
	const isCheckAll = computed(() => {
		return cartList.value.items.every(item => item.goods.every(child => child.checked == true ) == true)
	})
	const CHECKALL = () => {
		const isAll = cartList.value.items.every(item => {
			return item.goods.every(child => child.checked == true ) == true
		})
		cartList.value.items.forEach(item => {
			item.goods.forEach(child => child.checked = !isAll)
		})
	}
	// 选中某个店铺的所有商品
	/**
	 *  @param shopIndex 商铺的下标
	*/
	const CHECKSHOPALL = (shopIndex) => {
		// 先判断是否全选中这个商铺的所有商品 isAll 为true的话已全选/为false的话就是没有全部选中
		const isAll = cartList.value.items[shopIndex].goods.every(item => {
			return item.checked == true
		})
		cartList.value.items[shopIndex].goods.forEach(item => {
			item.checked = !isAll
		})
	}
	// 选中或取消单个商品
	/**
	 *  @param shopIndex 商铺的下标
	 *  @param goodsIndex 商品的下标
	*/
	const CHECKONE = (shopIndex, goodsIndex) => {
		console.log(cartList.value.items[shopIndex].goods[goodsIndex].checked);
		cartList.value.items[shopIndex].goods[goodsIndex].checked = !cartList.value.items[shopIndex].goods[goodsIndex].checked
	}
	const CLEARCARTLIST = () => {
		cartList.value = {
			coupon_amount: "0",
			delivery_amount: "0",
			delivery_money: "0",
			discount: '0',
			express_money: "0",
			give_integral_true: "0",
			goods_amount: "0",
			items: [],
			pay_amount: "0",
			shop_total_amount: "0",
			total_amount: "0"
		}
	}
	return {
		cartList,
		cartNum,
		checkedNum,
		checkedPrice,
		checkedList,
		GETCARTLIST,
		DELCARTGOODS,
		CHECKALL,
		CHECKSHOPALL,
		CHECKONE,
		isCheckAll,
		CLEARCARTLIST
	}
}, {
  // 数据持久化
	persist: {
		storage: {
			getItem: (key) => {
			  return uni.getStorageSync(key)
			},
			setItem: (key, value) => {
			  return uni.setStorageSync(key, value)
			},
		}
	}
})
